﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebChatDAL;
using System.Text;

namespace WebChat.User
{
    public partial class Chatters : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                WebChatDAO dao = new WebChatDAO();

                this.GridViewChatters.DataSource = dao.GetChatters();
                this.GridViewChatters.DataBind();
            }
        }

        protected string EvalUserName()
        {
            WebChatDAL.Chatter chatter = (WebChatDAL.Chatter)GetDataItem();

            return chatter.aspnet_Users.UserName;
        }

        protected string EvalOnlineStatus()
        {
            WebChatDAL.Chatter chatter = (WebChatDAL.Chatter)GetDataItem();

            DateTime lastLogin = chatter.aspnet_Users.aspnet_Membership.LastLoginDate;
            TimeSpan timeElapsedFromLastLogin = DateTime.Now - lastLogin;

            if (timeElapsedFromLastLogin.TotalMinutes <= 1)
            {
                return "online";
            }

            return "offline";
        }

        protected string EvalRoles()
        {
            WebChatDAL.Chatter chatter = (WebChatDAL.Chatter)GetDataItem();

            StringBuilder roles = new StringBuilder();

            if (chatter.aspnet_Users.aspnet_Roles.Count != 0)
            {
                roles.Append(chatter.aspnet_Users.aspnet_Roles.First().RoleName);

                foreach (aspnet_Roles role in chatter.aspnet_Users.aspnet_Roles.Skip(1))
                {
                    roles.Append(", ");
                    roles.Append(role.RoleName);
                }
            }

            return roles.ToString();
        }

        protected string SafeEval(string propertyName)
        {
            string value = (string)Eval(propertyName);
            string safeValue = Server.HtmlEncode(value);

            return safeValue;
        }

    }
}